1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security.Principal;
using VirtualRouterHost;
namespace Virtual_wifi
{
public partial class Form1 : Form
{
Process newprocess = new Process();
VirtualRouterHost.VirtualRouterHost virtualRouterHost;
List<ConnectedPeer> connectedPeersList;
Thread bgthread;
public Form1()
{
newprocess.StartInfo.UseShellExecute = false;
newprocess.StartInfo.CreateNoWindow = true;
newprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
InitializeComponent();
}
public bool IsUserAdminstrator()
{
bool isAdmin;
try
{
WindowsIdentity user = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(user);
isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (UnauthorizedAccessException)
{
isAdmin = false;
}
catch (Exception)
{
isAdmin = false;
}
return isAdmin;
}
public void Process_start_1()
{
Process_progressBar.Increment(25);
newprocess.StartInfo.FileName = "netsh";
newprocess.StartInfo.Arguments = "wlan stop hostednetwork";
try
{
using (Process execute = Process.Start(newprocess.StartInfo))
{
execute.WaitForExit();
Process_progressBar.Increment(25);
Process_start_2();
}
}
catch
{
//nothing
}
}
public void Process_start_2()
{
newprocess.StartInfo.FileName = "netsh";
newprocess.StartInfo.Arguments = "wlan set hostednetwork mode=allow ssid=" + SSID_textBox.Text + " key=" + Password_textBox.Text;
try
{
using (Process execute = Process.Start(newprocess.StartInfo))
{
execute.WaitForExit();
Process_progressBar.Increment(25);
Process_start_3();
}
}
catch
{
//nothing
}
}
public void Process_start_3()
{
newprocess.StartInfo.FileName = "netsh";
newprocess.StartInfo.Arguments = "wlan start hostednetwork";
try
{
using (Process execute = Process.Start(newprocess.StartInfo))
{
execute.WaitForExit();
int selectedIndex = comboBox1.SelectedIndex;
var conn = virtualRouterHost.GetSharableConnections();
List<SharableConnection> listOfSharableConnections = conn.ToList();
virtualRouterHost.Start(listOfSharableConnections[selectedIndex]);
Process_progressBar.Increment(25);
button_panel.Visible = true;
Play_Stop_button.Text = "Stop";
SSID_textBox.Enabled = false;
Password_textBox.Enabled = false;
comboBox1.Enabled = false;
bgthread = new Thread(() => ConnectedPeersTracker());
bgthread.Start();
}
}
catch
{
//nothing
}
}
public void Process_stop()
{
newprocess.StartInfo.FileName = "netsh";
newprocess.StartInfo.Arguments = "wlan stop hostednetwork";
try
{
Process_progressBar.Increment(50);
using (Process execute = Process.Start(newprocess.StartInfo))
{
execute.WaitForExit();
Process_progressBar.Increment(50);
button_panel.Visible = true;
Play_Stop_button.Text = "Start";
SSID_textBox.Enabled = true;
Password_textBox.Enabled = true;
comboBox1.Enabled = true;
bgthread.Abort();
}
}
catch
{
//nothing
}
}
private void button1_Click(object sender, EventArgs e)
{
if (Play_Stop_button.Text == "Start")
{
if (SSID_textBox.TextLength < 1)
{
MessageBox.Show("SSID Must be 1 character or more", "SSID Error");
return;
}
else if (Password_textBox.TextLength < 8)
{
MessageBox.Show("Password Must be 8 characters or more", "Password Error");
return;
}
else if (comboBox1.SelectedItem.ToString()=="None" || comboBox1.SelectedItem.ToString()=="")
{
MessageBox.Show("Select an adapter", "Adapter Error");
return;
}
button_panel.Visible = false;
Process_start_1();
}
else
{
button_panel.Visible = false;
Process_stop();
}
}
private void Form1_Load(object sender, EventArgs e)
{
virtualRouterHost = new VirtualRouterHost.VirtualRouterHost();
connectedPeersList = new List<ConnectedPeer>();
foreach (var conn in virtualRouterHost.GetSharableConnections())
{
comboBox1.Items.Add(conn.Name);
}
if (!IsUserAdminstrator())
{
MessageBox.Show("Run as Administrator", "Administrator Privilages Required", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
System.Environment.Exit(0);
}
}
private void label3_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void ConnectedPeersTracker()
{
//while (true)
//{
// foreach (var connPeers in virtualRouterHost.GetConnectedPeers())
// {
// listBox1.Items.Add(connPeers.MacAddress.ToString());
// }
// Thread.Sleep(200);
// listBox1.Items.Clear();
// //listBox1.
//}
var peers = virtualRouterHost.GetConnectedPeers();
while (true)
{
label6.Text = "Peers Connected (" + peers.Count().ToString() + "):";
foreach (var p in peers)
{
listBox1.Items.Add(p.MacAddress.ToString());
}
Thread.Sleep(200);
listBox1.Items.Clear();
}
}
//private bool isPeerAlreadyConnected(ConnectedPeer peer)
//{
// foreach (var element in listBox1.Items)
// {
// var elem;// = element as PeerDevice;
// if (elem != null)
// {
// if (elem.Peer.MacAddress.ToLowerInvariant() == peer.MacAddress.ToLowerInvariant())
// {
// return true;
// }
// }
// }
// return false;
//}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void exListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Really Quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Process_stop();
Application.Exit();
}
}
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("How to Use the Program : ", "Help", MessageBoxButtons.OK);
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Created by : \n\tNikhil N\n\tNikith Shetty", "About", MessageBoxButtons.OK);
//AboutBox.ActiveForm.Activate();
}
}
}